# equipment_tab.py
import tkinter as tk
from tkinter import ttk
from equipment_logic import initialize_dropdowns, on_combobox_select, update_text_display
from zbqz import prefix_suffix_options  # 确保从 zbqz.py 导入 prefix_suffix_options

class EquipmentTab(ttk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.setup_complete = {}  # 初始化 setup_complete 属性
        self.create_widgets()

    def create_widgets(self):
        self.label_title = tk.Label(self, text="装备预览")
        self.label_title.pack(pady=10)

        self.notebook = ttk.Notebook(self)
        self.notebook.pack(expand=True, fill="both", padx=10, pady=10)

        equipment_names = list(prefix_suffix_options.keys())  # 使用导入的 prefix_suffix_options
        self.tabs = {}
        for name in equipment_names:
            new_tab = ttk.Frame(self.notebook)
            self.notebook.add(new_tab, text=name)
            self.tabs[name] = new_tab

        self.notebook.bind("<<NotebookTabChanged>>", self.on_tab_change)
        self.on_tab_change(None)

    def on_tab_change(self, event):
        current_equipment = self.notebook.tab(self.notebook.index(self.notebook.select()), "text")
        if not self.setup_complete.get(current_equipment, False):
            # 确保 current_equipment 是 prefix_suffix_options 的一个键
            if current_equipment in prefix_suffix_options:
                prefix_options = prefix_suffix_options[current_equipment].get("前缀", [])
                suffix_options = prefix_suffix_options[current_equipment].get("后缀", [])
                # 现在调用 initialize_dropdowns 函数，确保接收所有返回值
                # 假设 initialize_dropdowns 现在返回 6 个值
                self.combos, self.text_display, self.selected_advanced, self.selected_supreme, self.attribute_selected, additional_value = initialize_dropdowns(
                    self, current_equipment, prefix_options, suffix_options
                )
                self.setup_complete[current_equipment] = True
            else:
                print(f"No options found for {current_equipment}")
                # 处理 current_equipment 不在 prefix_suffix_options 中的情况

    def update_text_display(self, event=None):
        update_text_display(self.combos, self.text_display)

    def add_weapon_subtype_tabs(self, parent_tab, weapon_types):
        for weapon_type in weapon_types:
            subtype_tab = ttk.Frame(parent_tab)
            subtype_tab.pack(expand=True, fill="both", padx=10, pady=10)
            weapon_label = tk.Label(subtype_tab, text=weapon_type)
            weapon_label.pack(pady=10)
            parent_tab.add(subtype_tab, text=weapon_type)

    def add_new_dropdown(self, parent_frame, options, pady):
        """
        在指定的标签页中添加新的下拉选项框。
        
        参数:
            parent_frame: 父容器对象。
            options: 下拉选项列表。
            pady: 下拉框距离文本框的垂直距离。
        """
        new_combo_label = tk.Label(parent_frame, text="武器类型")
        new_combo_label.pack(pady=(pady, 0))

        new_combo_var = tk.StringVar(value='')
        new_combo = ttk.Combobox(parent_frame, textvariable=new_combo_var, values=options, width=40, state='readonly')
        new_combo.pack(pady=2)
        return new_combo

# 在主程序中调用 add_weapon_subtype_tabs 方法
def main():
    root = tk.Tk()
    equipment_tab = EquipmentTab(root)
    equipment_tab.pack(expand=True, fill="both")
    weapon_types = ["双手剑", "双手锤", "双手斧", "弓", "火枪", "炮", "弩", "锡杖", "武仗", "匕首", "爪", "单手锤", "单手剑", "单手斧", "手枪", "法杖", "手仗", "魔杖", "灵仗"]
    equipment_tab.add_weapon_subtype_tabs(equipment_tab.notebook, weapon_types)
    root.mainloop()

if __name__ == "__main__":
    main()